package com.paradogs.framework.core.netty.clients.common;

import com.paradogs.framework.core.msg.ByteData;
import com.paradogs.framework.core.netty.PRConnHolder;
import com.paradogs.framework.core.netty.clients.PRAbClientMsgHandler;
import com.paradogs.framework.core.netty.server.PRConnection;
import com.paradogs.framework.core.utils.MsgUtils;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author: yumi
 * @date: 2023/11/4  15:52
 * @Description: TODO
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class PRServerClientMsgHandler extends PRAbClientMsgHandler<ByteData> {

    private ClientsProperties.Client properties;

    public PRServerClientMsgHandler(ClientsProperties.Client properties) {
        super(properties.getType());
        this.properties = properties;
    }

    @Override
    protected void processMsg(ByteData msg) throws Exception {
        MsgUtils.adapterHandle(msg);
    }

    /**
     * 连接建立事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        log.info("ServerClient connect success: {}", properties);
    }

    /**
     * 连接断开事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        log.info("ServerClient disconnect: {}", properties);
    }

}
